package org.jetlinks.community.device.util;

import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;

public class DateUtils {

    public static String yyyyMMddHHmmssToyyyyMMddTHHmmssXXX(String time, LocalDateTime defaultTime) {
        LocalDateTime target;
        if (StringUtils.isEmpty(time)) {
            target = defaultTime;
        } else  {
            target = LocalDateTime.parse(time, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        }
        Date startTimeDate = Date.from(target.atZone( ZoneId.systemDefault()).toInstant());
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
        return simpleDateFormat.format(startTimeDate);
    }

}
